<template>
  <view class="ss-shadow ss-r-20 list">
    <view
      class="ss-size ss-font-28 bottom"
      v-for="(i, index) in state.orderList"
      :key="index"
      @click="changeTab(i.id, i.url)"
    >
      <view class="ss-p-26 ss-flex ss-col-center ss-row-between">
        <view class="ss-flex">
          <view class="">
            <image :src="i.icon" mode="" class="image ss-m-x-10 ss-size"></image>
          </view>
          <view class="ss-x-20"> {{ i.title }} </view>
        </view>
        <uv-icon name="arrow-right" color="#757575" size="18" class="arrow-right" />
      </view>
    </view>
  </view>

  <view
    class="ss-r-60 ss-size ss-shadow ss-font-28 outLogin ss-p-30"
    @click="handleLogOut"
  >
    退出账号
  </view>
</template>
<script setup>
import { reactive, onMounted } from "vue";
import { getLogOut } from "@/request/login.js";
import { clearTaskCache } from "@/request/common.js";
const state = reactive({
  orderList: [
    {
      id: 0,
      title: "个人资料",
      icon: "/static/user/icon_user.png",
      url: "/pages/user/myInfo",
    },
    {
      id: 1,
      title: "修改密码",
      icon: "/static/user/icon_pwd.png",
      url: "/pages/login/forgotPwd?type=update",
    },
    {
      id: 2,
      title: "清空缓存",
      icon: "/static/user/icon_clear.png",
      url: "cache",
    },
  ],
  cacheSize: "0B", // 缓存大小
});

onMounted(() => {
  getCache();
});

// 跳转
const changeTab = (e, url) => {
  if (url == "cache") {
    // 清理缓存
    // #ifdef APP-PLUS
    let os = plus.os.name;
    if (os == "Android") {
      let main = plus.android.runtimeMainActivity();
      let sdRoot = main.getCacheDir();
      let files = plus.android.invoke(sdRoot, "listFiles");
      let len = files.length;
      for (let i = 0; i < len; i++) {
        let filePath = "" + files[i];
        plus.io.resolveLocalFileSystemURL(
          filePath,
          (entry) => {
            if (entry.isDirectory) {
              entry.removeRecursively(
                (entry) => {
                  uni.showToast({
                    title: `已清理${state.cacheSize}`,
                    icon: "success",
                  });
                  getCache();
                },
                (e) => {
                  console.log(e.message);
                }
              );
            } else {
              entry.remove();
            }
          },
          (e) => {
            console.log("文件路径读取失败");
          }
        );
      }
    } else {
      plus.cache.clear(() => {
        uni.showToast({
          title: `已清理${state.cacheSize}`,
          icon: "success",
        });
        getCache();
      });
    }
    // #endif
    // #ifdef H5
    uni.showToast({
      title: `已清理${state.cacheSize}`,
      icon: "success",
    });
    getCache();
    // #endif
  } else {
    uni.navigateTo({
      url,
    });
  }
};

// 获取应用的缓存大小
const getCache = () => {
  // #ifdef APP-PLUS
  plus.cache.calculate((size) => {
    if (size < 1024) {
      state.cacheSize = size + "B";
    } else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
      state.cacheSize = Math.floor((size / 1024) * 100) / 100 + "KB";
    } else if (size / 1024 / 1024 >= 1) {
      state.cacheSize = Math.floor((size / 1024 / 1024) * 100) / 100 + "M";
    }
  });
  // #endif
  // #ifdef H5
  state.cacheSize = "0B";
  // #endif
};

// 退出账号
const handleLogOut = () => {
  let list = uni.getStorageSync("offline-task-submit");
  let fails = uni.getStorageSync("offline-task-fail");
  if ((list && list.length > 0) || (fails && fails.length > 0)) {
    uni.showToast({
      title: "请先提交离线任务",
      icon: "none",
    });
    return;
  }
  uni.showModal({
    title: "退出",
    content: "确定要退出当前账号？",
    confirmText: "退出登录",
    showCancel: true,
    success: ({ confirm, cancel }) => {
      if (confirm) {
        getLogOut().then((res) => {
          if (res.code == 200) {
            uni.showToast({
              title: "退出成功",
              icon: "success",
              success: () => {
                uni.removeStorageSync("userInfo");
                uni.removeStorageSync("TOKEN");
                clearTaskCache();
                setTimeout(() => {
                  uni.reLaunch({
                    url: "/pages/login/index",
                  });
                }, 2000);
              },
            });
          }
        });
      }
    },
  });
};
</script>
<style lang="scss" scoped>
@import "@/uni.scss";

.add {
  width: 90%;
  margin: 30rpx auto;
}

.list {
  width: 90%;
  margin: 30rpx auto;
  overflow: hidden;

  .image {
    margin-top: 2rpx;
    width: 40rpx;
    height: 40rpx;
  }

  .arrow-right {
    width: 5%;
  }
}

.bottom {
  border-bottom: 2rpx solid #f2f2f2;
  background-color: #fff;
  &:active {
    background-color: #e6f1e9;
  }
}
.outLogin {
  background: #4ea770;
  color: #fff;
  text-align: center;
  position: fixed;
  bottom: 40rpx;
  left: 40rpx;
  right: 40rpx;
  box-shadow: 0 12rpx 30rpx 2rpx rgba(78, 167, 112, 0.5);
}
:deep(.uni-modal) {
  border-radius: 20rpx;
}
</style>
